package com.yalemang.uindicator;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class Adapter<VH extends Adapter.BaseViewHolder> extends RecyclerView.Adapter<VH> {

    int selectPosition = 0;
    List<UniversalIndicator.OnItemClickListener> onItemClickListeners = new ArrayList<>();
    List<UniversalIndicator.OnSelectListener> onSelectListeners = new ArrayList<>();
    //true:响应状态变化
    boolean isClickResponse = true;
    private boolean isReSelect = false;
    protected UniversalIndicator indicator;

    public void onAttach(UniversalIndicator indicator){
        this.indicator = indicator;
    }

    /**
     * 调整滑动定位偏差
     * @param isPositiveScroll
     * @return
     */
    protected int locationDeviation(boolean isPositiveScroll){
        return selectPosition;
    }

    protected int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        Log.d("Ellen2020", "setSelectPosition:" + selectPosition);
        Log.d("Ellen2020", "select  = " + this.selectPosition);
        Log.d("Ellen2020", "current  = " + selectPosition);
        isReSelect = false;
        if(this.selectPosition == selectPosition){
            isReSelect = true;
        }
        if (this.selectPosition == -1) {
            //首次
            this.selectPosition = selectPosition;
            notifyItemChanged(this.selectPosition);
            return;
        }
        if (this.selectPosition >= 0) {
            //第二次点击
            notifyItemChanged(this.selectPosition);
            this.selectPosition = selectPosition;
            notifyItemChanged(this.selectPosition);
        }
    }

    /**
     * 内部点击调用
     *
     * @param selectPosition
     */
    private void clickAdjustPosition(int selectPosition) {
        Log.d("Ellen2020", "clickAdjustPosition:" + selectPosition);
        Log.d("Ellen2020", "select  = " + this.selectPosition);
        Log.d("Ellen2020", "current  = " + selectPosition);
        isReSelect = false;
        if(this.selectPosition == selectPosition){
            isReSelect = true;
        }
        if (this.selectPosition == -1) {
            //首次
            this.selectPosition = selectPosition;
            notifyItemChanged(this.selectPosition);
            return;
        }
        if (this.selectPosition >= 0) {
            //第二次点击
            notifyItemChanged(this.selectPosition);
            this.selectPosition = selectPosition;
            notifyItemChanged(this.selectPosition);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        if (holder.isHaveStatus(position)) {
            if (selectPosition == position) {
                if(isReSelect){
                    reSelectStatus(holder, position);
                }else {
                    selectStatus(holder, position);
                }
            } else {
                unSelectStatus(holder, position);
            }
        }
        isReSelect = false;
        final int copyIndex = position;
        holder.itemView.setOnClickListener(v -> {
            for (UniversalIndicator.OnItemClickListener onItemClickListener : onItemClickListeners) {
                onItemClickListener.itemClick(v, copyIndex);
            }
            if (holder.isHaveStatus(copyIndex)) {
                if (selectPosition == copyIndex) {
                    for (UniversalIndicator.OnSelectListener onSelectListener : onSelectListeners) {
                        onSelectListener.onReSelectPosition(copyIndex);
                    }
                } else {
                    for (UniversalIndicator.OnSelectListener onSelectListener : onSelectListeners) {
                        onSelectListener.onSelectPosition(selectPosition, copyIndex);
                    }
                }
                if (isClickResponse) {
                    clickAdjustPosition(copyIndex);
                }
            } else {
                for (UniversalIndicator.OnSelectListener onSelectListener : onSelectListeners) {
                    onSelectListener.onUnStatusClick(copyIndex);
                }
            }
        });
    }

    public static class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        //是否具有状态
        public boolean isHaveStatus(int position) {
            return true;
        }

        protected <T extends View> T findViewById(int id) {
            return itemView.findViewById(id);
        }
    }

    protected abstract void selectStatus(VH holder, int position);

    protected abstract void unSelectStatus(VH holder, int position);

    protected void reSelectStatus(VH holder, int position) {
        selectStatus(holder, position);
    }
}
